package hsmoto.core
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;

	/**
	 * 这个类主要实现自动的直接方法，也就是说自动布局对象的位置，最终由此类的静态方法实现
	 * @author Zhang
	 * 
	 */	
	public class Tool
	{
		private static const TOOL:BitmapData = new BitmapData(1, 1);
		/**
		 * 让大小设置立即生效 (正常情况scrollRect属性需要下一帧才会生效)
		 */		
		public static function updataSize(target:UIComponent):void
		{
			TOOL.draw(target);
		}
		
		private static var w:Number = 0;
		private static var h:Number = 0;
		/**
		 * 添加计算显示对象内部合适范围的子对象，通过getRect方法获取结果
		 */		
		public static function addToRect(x:Number, y:Number, width:Number, height:Number):void
		{
			var ww:Number = x + width;
			if(ww > w)
				w = ww;
			var hh:Number = y + height;
			if(hh > h)
				h = hh;
		}
		/**
		 * 计算显示对象内部刚好放下指定的子对象的合适范围，通过addToRect方法添加指定的子对象
		 */		
		public static function getRect(target:DisplayObject):Rectangle
		{
			var rect:Rectangle = new Rectangle(0, 0, w * target.scaleX, h * target.scaleY);
			w = 0;
			h = 0;
			return rect;
		}
		/**
		 * 获取包含缩放的显示对象的范围 
		 */		
		public static function getScaleBounds(targetCoordinateSpace:DisplayObject):Rectangle
		{
			var rect:Rectangle = targetCoordinateSpace.getBounds(targetCoordinateSpace);
			rect.x = rect.x * targetCoordinateSpace.scaleX;
			rect.y = rect.y * targetCoordinateSpace.scaleY;
			rect.width = rect.width * targetCoordinateSpace.scaleX;
			rect.height = rect.height * targetCoordinateSpace.scaleY;
			return rect;
		}
		/*
		 * 下面的所有方法实现对应相对位置x、y的具体实现过程  
		 * @param target 需要布局的对象
		 * @param value 属性的值
		 * @param rect1 target的范围
		 * @param rect2 target父容器的范围
		 * 
		 */			
		public static function x(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			if(target is UIComponent)
			{
				(target as UIComponent).currentX = value;
			}
			else
			{
				target.x = value;
			}
		}
		
		public static function y(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			if(target is UIComponent)
			{
				(target as UIComponent).currentY = value;
			}
			else
			{
				target.y = value;
			}
		}
		
		public static function normal(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			
		}
		
		public static function top(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			if(target is UIComponent)
			{
				(target as UIComponent).currentY = value - rect1.y;
			}
			else
			{
				target.y = value - rect1.y;
			}
		}
		
		public static function bottom(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			if(target is UIComponent)
			{
				(target as UIComponent).currentY = rect2.height + rect2.y - value - rect1.height - rect1.y;
			}
			else
			{
				target.y = rect2.height + rect2.y - value - rect1.height - rect1.y;
			}
		}
		
		public static function verticalCenter(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			if(target is UIComponent)
			{
				(target as UIComponent).currentY = (rect2.height + rect2.y - rect1.height) / 2 - rect1.y + value;
			}
			else
			{
				target.y = (rect2.height + rect2.y - rect1.height) / 2 - rect1.y + value;
			}
		}
		
		public static function left(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			if(target is UIComponent)
			{
				(target as UIComponent).currentX = value - rect1.x;
			}
			else
			{
				target.x = value - rect1.x;
			}
		}
		
		public static function right(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			if(target is UIComponent)
			{
				(target as UIComponent).currentX = rect2.width + rect2.x - value - rect1.width - rect1.x;
			}
			else
			{
				target.x = rect2.width + rect2.x - value - rect1.width - rect1.x;
			}
		}
		
		public static function horizontalCenter(target:DisplayObject, value:Number, rect1:Rectangle, rect2:Rectangle):void
		{
			if(target is UIComponent)
			{
				(target as UIComponent).currentX = (rect2.width + rect2.x - rect1.width) / 2 - rect1.x + value;
			}
			else
			{
				target.x = (rect2.width + rect2.x - rect1.width) / 2 - rect1.x + value;
			}
		}
	}
}